package com.xuecheng.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class GlobalCorsConfig {
    /**
     * 允许跨域调用的过滤器
     */
    @Bean
    public CorsFilter corsFilter(){//public CorsFilter(CorsConfigurationSource configSource)
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许所有请求方法跨域调用
        corsConfiguration.addAllowedMethod("*");
        //允许白名单域名进行跨域调用
        corsConfiguration.addAllowedOrigin("*");
        //放行全部原始头信息
        corsConfiguration.addAllowedHeader("*");
        //允许跨越发送cookie
        corsConfiguration.setAllowCredentials(true);
//        public void registerCorsConfiguration(String path, CorsConfiguration config)
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
}
